package com.examples.neo4j;


import com.alibaba.fastjson.JSON;
import jdk.internal.org.objectweb.asm.Handle;
import org.neo4j.driver.*;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Neo4jDemo {
    public static void main(String[] args) throws Exception {
        // Neo4j数据库的URI（假设是本地默认端口）
        String uri = "bolt://192.168.0.117:7687";

        // Neo4j数据库的用户名和密码
        String user = "neo4j";
        String password = "12345678";

        // 创建一个Driver实例用于建立连接
        try (Driver driver = GraphDatabase.driver(uri, AuthTokens.basic(user, password))) {
            // 使用Driver创建Session以执行Cypher查询
            try (Session session = driver.session()) {
                // 执行一个简单的Cypher查询并打印结果
                String query = "MATCH (n) RETURN n LIMIT 1";
                List<Value> values = session.run(query).single().values();
                System.out.println("Nodes: " + session.run(query).single().get(0).asNode().toString());
            }
        } catch (Exception e) {
            System.err.println("Error connecting to Neo4j: " + e.getMessage());
        }


        // 创建一个实体
        /*try (Driver driver = GraphDatabase.driver(uri, AuthTokens.basic(user, password))) {
            try (Session session = driver.session()) {
                String s = "CREATE (Kean343322u:Person466ih {name:'Keanu Reeves111', born:1964, restId:'23323'})";
                session.run(s);
                System.out.println("Run SQL to Neo4j: "+s);
            }
        } catch (Exception e) {
            System.err.println("Error connecting to Neo4j: " + e.getMessage());
        }*/

        /// 实体put 存在则更新，否则直接插入
        HashMap<String, String> hashMap = new HashMap<String, String>() {{
            put("id", "123123");
            put("name", "Reeves114");
            put("born", "1924");
            put("age", "23314");
        }};
        String type = "Person466ih";
        try (Driver driver = GraphDatabase.driver(uri, AuthTokens.basic(user, password))) {
            try (Session session = driver.session()) {
                String s1 = "MATCH (" + type + "_" + hashMap.get("id") + ":" + type + " {id: '" + hashMap.get("id") + "'}) RETURN id(" + type + "_" + hashMap.get("id") + ") AS nodeId";
                Result run = session.run(s1);
                System.out.println("Run SQL to Neo4j: "+s1);
                StringBuilder sb = new StringBuilder();
                boolean isFirst = true;
                for (Map.Entry<String, String> entry : hashMap.entrySet()) {
                    if (!isFirst) {
                        sb.append(", ");
                    } else {
                        isFirst = false;
                    }
                    sb.append(entry.getKey()).append(":").append("'").append(entry.getValue()).append("'");
                }
                String values = sb.toString();
                if (run.hasNext()) {
                    // 节点存在，获取节点ID
                    String s = "MATCH (p) WHERE p.id = '" + hashMap.get("id") + "' SET p = " + "{" + values + "}";
                    session.run(s);
                    System.out.println("Run SQL to Neo4j: "+s);
                }else{
                    // 节点不存在，创建节点
                    String s = "CREATE ("+type+"_"+hashMap.get("id")+":"+type+" {"+ values +"})";
                    session.run(s);
                    System.out.println("Run SQL to Neo4j: "+s);
                }
            }
        } catch (Exception e) {
            System.err.println("Error connecting to Neo4j: " + e.getMessage());
        }


    }
}
